package com.ms.service.module.zone.action;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ms.service.error.ErrorCode;
import com.ms.service.module.zone.message.CPlayerSwitchZoneRequest;
import com.ms.service.module.zone.message.SPlayerSwitchZoneResponse;
import com.ms.service.module.zone.module.BasicZoneModule;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.message.MessageTypes;
import com.xpec.c4.service.annotations.MessageType;
import com.xpec.c4.service.annotations.Name;
import com.xpec.c4.service.common.BaseMessageAction;
import com.xpec.c4.service.contexts.Contexts;
import com.xpec.c4.service.contexts.ScopeType;
import com.xpec.c4.service.message.MessageAction;
import com.xpec.c4.service.module.common.PlayerCommonInfoManager;


/**
 * 接收 換 zone 相關消息的 action
 * 
 * @author TW15010
 * 
 */
@Name(scopeType = ScopeType.SESSION)
public class SwitchZoneAction extends BaseMessageAction
{
	@SuppressWarnings("unused")
	private static final Log log = LogFactory.getLog(SwitchZoneAction.class);
	
	/**
	 * client 要求換zone消息
	 * @param request
	 */
	@MessageType(typeCode = MessageTypes.C_PLAYER_SWITCH_ZONE_REQUEST)
	public void revClientSwitchZoneRequest(CPlayerSwitchZoneRequest request)
	{
		if(getPlayer()==null)
		{
			SPlayerSwitchZoneResponse response = new SPlayerSwitchZoneResponse();
			response.setErrorCode(ErrorCode.PLAYER_NOT_FOUND);
			MessageAction.sendMessage( getSessionID(), MessageTypes.S_ZONE_SWITCH_VERIFYCODE_RESPONSE, response);
		}
		else
		{
				BasicZoneModule basicZoneModule = (BasicZoneModule) Contexts
						.getModule();
				
				basicZoneModule.getZone().onPlayerSwitchZoneRequest(request , getSessionID() , getPlayer().getGuid());
		}
	}
	
	/**
	 * zone 要求換zone消息
	 * @param request
	 */
	@MessageType(typeCode = MessageTypes.S_ZONE_SWITCH_ZONE_REQUEST)
	public void revZoneSwitchZoneRequest(CPlayerSwitchZoneRequest request)
	{
		BasicZoneModule basicZoneModule = (BasicZoneModule) Contexts
				.getModule();
		
		basicZoneModule.getZone().onZoneSwitchZoneRequest(request , getSrcService());
	}
	
	/**
	 * 新zone傳送verifycode給舊zone
	 * @param request
	 */
	@MessageType(typeCode = MessageTypes.S_ZONE_SWITCH_VERIFYCODE_BACK)
	public void revZoneSwitchVerifyCodeRequest(CPlayerSwitchZoneRequest request)
	{
		if(request.getErrorCode() != ErrorCode.SUCCESS)
		{
			SPlayerSwitchZoneResponse response = new SPlayerSwitchZoneResponse();
			response.setErrorCode(request.getErrorCode());
			MessageAction.sendMessage( getSessionID(), MessageTypes.S_ZONE_SWITCH_VERIFYCODE_RESPONSE, response);
		}
		else
		{
			BasicZoneModule basicZoneModule = (BasicZoneModule) Contexts
					.getModule();
			
			//sessionid 會變成S to S 的sessionid ，所以要額外使用帶玩家的sessionid
			GUID sessionid = PlayerCommonInfoManager.getInstance().getSessionByPlayerGuid(request.getUserId());
			basicZoneModule.getZone().onZoneSwitchVerifyCodeRequest(request , sessionid);
		}
	}
	
}
